const Op = require('sequelize').Op;
const Joi = require('@hapi/joi');
const Schema = require('../schema/config.schema')
const Model = require('../models/config.model')
module.exports = [
  {
    path: '/config',
    method: 'GET',
    handler: (request, h) => {
      const page = request.query.page || 1;
      const sort = request.query.sort || Model.pk || 'id';
      const desc = (request.query.descending  === 'true') || false;
      const page_size = request.query.page_size || 20;
      return Model.findAndCountAll({
        offset: (page - 1) * page_size,
        limit: parseInt(page_size),
        order: [[sort, desc ? 'desc' : 'asc']],
        where: {}
      }).then((result) => {
        return {
          total: result.count,
          per_page: 20,
          current_page: page,
          last_page: Math.ceil(result.count / 20),
          data: result.rows
        };
      }).catch((err) => {
        return h.response(err.message).code(500);
      });
    },
    options: {
      tags: ['api', 'Content/Config'],
      description: '获取配置参数列表',
      notes: '根据查询条件获取配置参数列表',
      validate: {
        query: Joi.object({
          page: Joi.number().integer().positive().min(1).default(1).description('分页页码'),
          sort: Joi.string().default('id').description('排序字段'),
          descending: Joi.boolean().default('false').description('是否desc'),
          page_size: Joi.number().integer().min(-1).default(20).description('分页容量')
        }).label('SearchCondition')
      },
      response: {
        schema: Joi.object({
          total: Joi.number().min(0),
          per_page: Joi.number().default(20),
          current_page: Joi.number().min(1),
          last_page: Joi.number().min(0),
          data: Joi.array().items(Schema.Default)
      }).label('ContentList')
      }
    }
  },
  {
    method: 'GET',
    path: '/config/{id}',
    handler: (request, h) => {
      return Model.findByPk(request.params[Model.pk]).then((content) => {
        return content ? content : h.response('未找到！').code(500);
      });
    },
    options: {
      tags: ['api', 'Content/Config'],
      description: '获取配置参数详情',
      notes: '根据ID获取配置参数列表',
      validate: {
        params: Joi.object({
          id: Joi.number().integer().positive().required().description('主键(id)')
        })
      },
      response: {
        schema: Schema.Default
      }
    }
  },
  {
    method: 'POST',
    path: '/config',
    handler: (request, h) => {
      return Model.create(request.payload).then((content) => {
          return '添加成功！';
      }).catch((err) => {
          request.log('debug', err);
          return h.response(err.message).code(500);
      });
    },
    options: {
      tags: ['api', 'Content/Config'],
      description: '新增配置参数',
      notes: '提交新增配置参数信息',
      validate: {
        payload: Schema.Create
      },
      response: {
        schema: Joi.string().example('新增成功！')
      }
    }
  },
  {
    method: 'DELETE',
    path: '/config/{id}',
    handler: (request, h) => {
      return Model.destroy({
        where: {
          [Model.pk]: request.params[Model.pk]
        }
      }).then((res) => {
        if (res === 1) {
            return '删除成功！';
        }
        return h.response('删除失败！找不到相关记录').code(500);
      }).catch((err) => {
        request.log('debug', err);
        return h.response(err.message).code(500);
      });
    },
    options: {
      tags: ['api', 'Content/Config'],
      description: '删除配置参数',
      notes: '根据ID获取配置参数列表',
      validate: {
        params: Joi.object({
          id: Joi.number().integer().positive().required().description('主键(id)')
        })
      },
      response: {
        schema: Joi.string().example('删除成功！')
      }
    }
  },
  {
    method: 'PUT',
    path: '/config/{id}',
    handler: (request, h) => {
      return Model.update(request.payload, {
        where: {
          [Model.pk]: request.params[Model.pk]
        }
      }).then((res) => {
        if (res[0] === 1) {
          return '更新成功！';
        }
        return '更新失败！';
      }).catch((err) => {
        request.log('debug', err);
        return h.response(err.message).code(500);
      });
    },
    options: {
      tags: ['api', 'Content/Config'],
      description: '更新配置参数',
      notes: '根据id更新配置参数信息',
      validate: {
        payload: Schema.Update
      },
      response: {
        schema: Joi.string().example('更新成功！')
      }
    }
  }
]